<script setup>
import {ref, onMounted} from 'vue'
import {ArrowRight} from '@element-plus/icons-vue'
import {listByLatestOrHot, queryPageByCategory} from "../../service/infoNewsApi.js";
import {listActivityLatest} from "../../service/activityApi.js";
import {pageInfoCategory} from "../../service/infoCategoryApi.js";

// 模拟数据
const mockCarouselItems = [
  {img: 'https://ai-public.mastergo.com/ai/img_res/df8ac042028d124e5dc0c692b2c6911e.jpg'},
  {img: 'https://ai-public.mastergo.com/ai/img_res/84cf76a64a80917d0b3106961b5b2483.jpg'},
  {img: 'https://ai-public.mastergo.com/ai/img_res/5dc597932a5c9abbda38466729424660.jpg'}
]

const mockNewsList = [
  {
    id: '1',
    title: '市卫生健康委领导参观历城区养老服务中心，肯定医养结合服务模式',
    summary: '近日，市卫生健康委领导一行莅临历城区养老服务中心参观指导工作，对中心的医养结合服务模式给予高度评价...',
    time: '2023-12-29 16:30:06',
    viewCount: 1280,
    comments: 38,
    cover: 'https://ai-public.mastergo.com/ai/img_res/df8ac042028d124e5dc0c692b2c6911e.jpg'
  },
  {
    id: '2',
    title: '特困老人的"温暖使者"：社区志愿者在行动',
    summary: '社区志愿者们组织开展"温暖使者"行动，为特困老人送去温暖与关怀。活动中，志愿者们不仅为老人送去了生活必需品...',
    time: '2023-12-29 16:28:55',
    viewCount: 965,
    comments: 26,
    cover: 'https://ai-public.mastergo.com/ai/img_res/df8ac042028d124e5dc0c692b2c6911e.jpg'
  },
  {
    id: '3',
    title: '特困老人的"温暖使者"：社区志愿者在行动',
    summary: '社区志愿者们组织开展"温暖使者"行动，为特困老人送去温暖与关怀。活动中，志愿者们不仅为老人送去了生活必需品...',
    time: '2023-12-29 16:28:55',
    viewCount: 965,
    comments: 26,
    cover: 'https://ai-public.mastergo.com/ai/img_res/df8ac042028d124e5dc0c692b2c6911e.jpg'
  }
]

const mockSideNews = [
  {id: '1', title: '养老服务质量提升工程启动，打造15分钟养老服务圈', time: '2024-01-02 10:30'},
  {id: '2', title: '我市将新建10家社区养老服务中心，提供全方位养老服务', time: '2024-01-02 09:15'},
  {id: '3', title: '智慧养老服务平台上线，让养老服务更便捷', time: '2024-01-01 16:45'},
  {id: '4', title: '养老机构服务标准化建设推进会召开', time: '2024-01-01 14:20'}
]

const mockActivities = [
  {
    id: '1',
    name: '爱心共建聚合力 关爱老人做公益',
    date: '2024-09-01',
    image: 'https://ai-public.mastergo.com/ai/img_res/67535be1879030c435714fe506c74fac.jpg'
  },
  {
    id: '2',
    name: '孝度集体生日会，让"银发"志愿者大家庭的温暖',
    date: '2024-09-01',
    image: 'https://ai-public.mastergo.com/ai/img_res/67535be1879030c435714fe506c74fac.jpg'
  }, {
    id: '3',
    name: '孝度集体生日会，让"银发"志愿者大家庭的温暖',
    date: '2024-09-01',
    image: 'https://ai-public.mastergo.com/ai/img_res/67535be1879030c435714fe506c74fac.jpg'
  }
]

const mockFilterOptions = [
  {id: '1', name: '全部'},
  {id: '2', name: '养老动态'},
  {id: '3', name: '养老政策'},
  {id: '4', name: '养老新闻'},
  {id: '5', name: '养老服务'},
]

// 数据引用
const carouselItems = ref(mockCarouselItems)
const newsList = ref(mockNewsList)
const sideNews = ref(mockSideNews)
const activities = ref(mockActivities)
const filterOptions = ref(mockFilterOptions)

const activeTab = ref('')
const activeSideTab = ref('1')
const infoNewsList = ref({
  searchValue: '', // 新增搜索值
  loading: false,
  tableData: [], // 数据列表
  multipleSelection: [], // 选中项
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 4, // 分页大小
  categoryList: [], // 分类列表
  content: ''//内容存储
})
// 初始化函数
const fetchData = async () => {
  await Promise.all([
    carouselItems.value = mockCarouselItems,
    // 获取资讯信息列表数据
    listInfoNewsByCategory(""),
    // 获取右侧资讯信息数据
    listInfoNewsLH(activeSideTab.value),
    // 获取公益活动数据
    listActivity(),
    // 获取过滤选项数据
    handleOption()
  ]);
};


// 获取资讯信息列表数据
const listInfoNewsByCategory = async (categoryId) => {
  activeTab.value = categoryId
  infoNewsList.value.loading = true;
  const params = {
    pageNum: infoNewsList.value.currentPage,
    pageSize: infoNewsList.value.pageSize,
    categoryId: activeTab.value,
  };
  const newsListResponse = await queryPageByCategory(params)
  newsList.value = newsListResponse.data.records || mockNewsList
  infoNewsList.value.total = newsListResponse.data.total || 10;
  infoNewsList.value.tableData = newsListResponse.data.records;
}

// 获取右侧资讯信息数据
const listInfoNewsLH = async (id) => {
  activeSideTab.value = id
  await listByLatestOrHot(id).then(response => {
    const sideNewsResponse = response.data
    sideNews.value = sideNewsResponse || mockSideNews
  })
      .catch(error => {
      });
}

// 获取公益活动数据
const listActivity = async () => {
  await listActivityLatest().then(response => {
    const activitiesResponse = response.data
    activities.value = activitiesResponse || mockActivities
  })
      .catch(error => {
        console.error(error);
      });
}

// 获取分类列表数据
const handleOption = async () => {
  const params = {
    pageNum: 1,
    pageSize: 5,
    name: '',
  };
  const filterOptionsResponse = await pageInfoCategory(params);
  filterOptions.value = filterOptionsResponse.data.records || mockFilterOptions
};

// 分页变化
const changePage = async (val) => {
  infoNewsList.value.currentPage = val;
  await listInfoNewsByCategory(activeTab.value);
};


// 组件挂载时调用初始化函数
onMounted(() => {
  fetchData()
})
</script>

<template>
  <div class="page-container">
    <el-row type="flex" :gutter="20">
      <!-- 左侧主内容区 -->
      <el-col :span="14">
        <!-- 轮播图 -->
        <el-carousel class="carousel-box">
          <el-carousel-item v-for="(item, i) in carouselItems" :key="i">
            <img :src="item.img" class="carousel-img">
          </el-carousel-item>
        </el-carousel>

        <!-- 资讯信息过滤选项卡 -->
        <div class="filter-tabs">
          <el-radio-group :model-value="activeTab.value" class="filter-info">
            <el-radio-button label="" @click="listInfoNewsByCategory('')">全部</el-radio-button>
            <el-radio-button v-for="option in filterOptions" :key="option.id" :label="option.id" @click="listInfoNewsByCategory(option.id)">
              {{ option.name }}
            </el-radio-button>
          </el-radio-group>
        </div>

        <!-- 资讯信息列表 -->
        <div class="news-list">
          <el-card v-for="(news, index) in newsList" :key="index" class="news-card">
            <router-link :to="`/client/infoDetail/${news.id}`" class="nav-link">
              <div class="news-content">
                <!-- 图片前置 -->
                <el-image
                    :src="news.cover"
                    class="news-image"
                    fit="cover"
                ></el-image>
                <div class="text-content">
                  <h3 class="news-title">{{ news.title }}</h3>
                  <p class="news-desc">{{ news.summary }}</p>
                  <div class="news-meta">
                    <span class="time">{{ news.time }}</span>
                    <el-tag type="info" size="small">浏览量：{{ news.viewCount }}</el-tag>
                    <el-tag type="success" size="small">评论数：{{ news.comments }}</el-tag>
                  </div>
                </div>
              </div>
            </router-link>
          </el-card>
        </div>

        <!-- 分页 -->
        <div class="pagination">
          <!--总数超过一页，再展示分页器-->
          <el-pagination
              background
              layout="prev, pager, next"
              :total="infoNewsList.total"
              :page-size="infoNewsList.pageSize"
              :current-page="infoNewsList.currentPage"
              @current-change="changePage"
          />
        </div>

      </el-col>

      <!-- 右侧边栏 -->
      <el-col :span="10">
        <!-- 最新/最热切换 -->
        <el-card class="side-card">
          <div class="side-header">
            <span :class="{active: activeSideTab == '1'}" @click="listInfoNewsLH(1)">最新</span>
            <span>|</span>
            <span :class="{active: activeSideTab == '2'}" @click="listInfoNewsLH(2)">最热</span>
          </div>
          <div class="side-news-list">
            <div v-for="(item, i) in sideNews" :key="i" class="side-news-item">
              <router-link :to="`/client/infoDetail/${item.id}`" class="nav-link">
                <div hidden>{{ item.id }}</div>
                <div class="side-news-title">{{ item.title }}</div>
                <div class="side-news-time">{{ item.time }}</div>
              </router-link>
            </div>
          </div>
        </el-card>

        <!-- 公益活动列表优化 -->
        <el-card class="side-card activity-card">
          <div class="side-header">
            <span>公益活动</span>
            <router-link to="/client/activityList" class="more-btn">
              <el-button type="text">更多
                <el-icon><ArrowRight/></el-icon>
              </el-button>
            </router-link>
          </div>
          <div class="activity-list">
            <el-card
                v-for="(act, i) in activities"
                :key="i"
                shadow="hover"
                class="activity-item"
            >
              <router-link :to="`/client/activity/${act.id}`" class="nav-link">
                <div class="activity-wrapper">
                  <el-image :src="act.image" class="activity-image"></el-image>
                  <div class="activity-details">
                    <h3>{{ act.name }}</h3>
                    <p class="activity-date">{{ act.date }}</p>
                  </div>
                </div>
              </router-link>
            </el-card>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
  <div class="footer">
    <div>社区养老服务整合平台</div>

  </div>
</template>

<style scoped>
/* 新增全局过渡效果 */
* {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
/* 落叶黄绿渐变调色板 */
:root {
  --leaf-green: #8BC34A;
  --autumn-yellow: #FFD54F;
  --dark-green: #689F38;
  --light-beige: #FFF3E0;
}

.page-container {
  width: 75%;
  padding: 30px;
  max-width: 1400px;
  margin: 0 auto;
}

/* 轮播图优化 */
.carousel-box {
  border-radius: 16px;
  margin-bottom: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  height: 360px; /* 增大高度 */
}

.carousel-img {
  height: 360px; /* 同步调整 */
  object-position: center 20%;
}

/* 过滤选项卡优化 */
.filter-tabs {
  margin: 30px 0;
}

.filter-tabs .el-radio-button {
  margin-right: 20px;
}

.filter-tabs .el-radio-button .el-radio-button__inner {
  border-radius: 24px;
  padding: 12px 24px;
  background: linear-gradient(145deg,#ffd299 , #B3E5FC); /* 天空蓝渐变 */
  border: none;
  color: #01579B;
  font-weight: 500;
}

/* 资讯列表优化 */
.news-list {
  margin-top: 30px;
}

.news-card {
  border-radius: 12px !important;
  border: none;
  background: linear-gradient(to right, #F8F9FA, #ffeba7); /* 浅色渐变 */
  transition: transform 0.3s;
  margin-bottom: 20px;
}

.news-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(33, 150, 243, 0.2);
}

.news-content {
  gap: 20px;
}

.side-news-title {
  font-size: 18px;
  color: #01579B;
  margin-bottom: 12px;
}

.news-desc {
  font-size: 14px;
  color: #546E7A;
  line-height: 1.8;
}

/* 分页器优化 */
.pagination {
  margin-left: 360px;
}

:deep(.el-pagination.is-background .btn-prev),
:deep(.el-pagination.is-background .btn-next),
:deep(.el-pagination.is-background .el-pager li) {
  border-radius: 8px;
  margin: 0 6px;
}

/* 右侧边栏优化 */
.side-card {
  border-radius: 16px !important;
  border: none;
  margin-bottom: 30px;
}

.side-header {
  padding: 16px;
  border-bottom: 2px solid rgba(2, 136, 209, 0.1);
}

.side-header span {
  font-size: 16px;
  padding: 8px 16px;
  border-radius: 8px;
  transition: all 0.3s;
}

.side-header span.active {
  background: rgba(33, 150, 243, 0.1);
  color: #ff4d4f;
}

.side-news-item {
  padding: 16px;
  margin: 8px 0;
  background: linear-gradient(145deg, #ffd299,#cee8ff) !important;
  border-radius: 8px;
}

.side-news-item:hover {
  transform: translateX(10px);
  box-shadow: 2px 4px 12px rgba(33, 150, 243, 0.1);
}

/* 公益活动卡片优化 */
.activity-item {
  background: linear-gradient(135deg, #F0FFF3, #E8F5E9) !important; /* 淡黄绿渐变 */
  border: none !important;
  border-radius: 12px !important;
  overflow: hidden;
  margin-bottom: 20px;
}

.activity-image {
  width: 120px !important;
  height: 90px !important;
  border-radius: 8px !important;
}

.activity-details h3 {
  color: #1B5E20;
  font-size: 14px;
}

.activity-date {
  color: #4CAF50;
}

/* 悬浮效果增强 */
.nav-link:hover {
  opacity: 0.9;
  transform: scale(0.98);
}

/* 按钮特效 */
.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
}

/* 响应式调整 */
@media (max-width: 992px) {
  .el-col {
    width: 100%;
    max-width: 100%;
  }

  .news-content {
    flex-direction: column;
  }

}
/* 分类按钮渐变调整 */
.filter-tabs .el-radio-button .el-radio-button__inner {
  background: linear-gradient(145deg, var(--autumn-yellow), var(--leaf-green)) !important;
  color: #333;
}

/* 资讯卡片布局优化 */
.news-content {
  display: flex;
  align-items: flex-start;
}

.news-image {
  width: 240px !important;
  height: 160px !important;
  min-width: 240px;
  border-radius: 12px;
  box-shadow: 2px 4px 12px rgba(0,0,0,0.1);
}

.text-content {
  flex: 1;
  margin-left: 16px;
}

.news-title {
  font-size: 18px;
  color: var(--dark-green);
  line-height: 1.4;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.news-desc {
  color: #666;
  line-height: 1.6;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.news-meta {
  display: flex;
  gap: 15px;
  align-items: center;
  margin-top: 40px;
}

/* 公益活动样式优化 */
.activity-wrapper {
  display: flex;
  gap: 20px;
  align-items: center;
}

.activity-image {
  width: 160px !important;
  height: 120px !important;
  border-radius: 8px;
}

.activity-details h3 {
  color: var(--dark-green);
  line-height: 1.4;
  margin-bottom: 8px;
}

/* 右侧边栏背景优化 */
.side-card {
  background: linear-gradient(145deg, #FFF9C4, var(--light-beige)) !important;
  border: 1px solid rgba(139, 195, 74, 0.15) !important;
}

/* 悬停效果增强 */
.news-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(139, 195, 74, 0.15);
}

.activity-item:hover {
  transform: translateX(5px);
}

@media (max-width: 992px) {
  .news-image {
    width: 100% !important;
    height: 200px !important;
  }

  .activity-wrapper {
    flex-direction: column;
  }

  .activity-image {
    width: 100% !important;
  }
}
.more-btn {
  margin-left: 340px;
}
.filter-info /deep/ .el-radio-button__inner {
  border-radius: 10px;
  background: linear-gradient(135deg, #a1c4fd, #c2e9fb);
  border: none;
  color: #01579B;
}
.filter-info /deep/ .el-radio-button__inner:hover {
  background: linear-gradient(45deg, #B3E5FC, #ffd299); /* 反转渐变 */
}
.footer {
  font-size: 14px;
  height: 100px; /* 根据需要调整高度 */
  border-top: 1px solid #e9e9e9;
  display: flex;
  flex-direction: row; /* 水平排列 */
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.beian-footer {
  bottom: 0;
  text-align: center;

a {
  color: #1D1D26;
  font-size: 14px;
  text-decoration-line: underline;

&:hover {
   color: #409EFF;
 }
}
}

</style>
